package com.beesoft.demo.controller;

import com.beesoft.demo.dto.PageResponse;
import com.beesoft.demo.entity.Language;
import com.beesoft.demo.repository.LanguageRepository;
import com.beesoft.demo.service.LanguageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/languages")
@RequiredArgsConstructor
public class LanguageController {

    private final LanguageRepository languageRepository;
    private final LanguageService languageService;

    @GetMapping
    public ResponseEntity<PageResponse<Language>> getAllLanguages(
            @RequestParam(required = false) String search,
            @RequestParam(name = "order_column", required = false, defaultValue = "id") String orderColumn,
            @RequestParam(name = "order_direction", required = false, defaultValue = "asc") String orderDirection,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(name = "per_page", defaultValue = "20") int perPage) {
        
        // 验证分页参数
        if (page < 1) page = 1;
        if (perPage < 1) perPage = 20;
        if (perPage > 100) perPage = 100; // 最大每页100条
        
        PageResponse<Language> response = languageService.searchLanguages(
                search, orderColumn, orderDirection, page, perPage);
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/all")
    public ResponseEntity<List<Language>> getAllLanguagesWithoutPagination() {
        return ResponseEntity.ok(languageRepository.findAll());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Language> getLanguageById(@PathVariable Long id) {
        return languageRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/code/{code}")
    public ResponseEntity<Language> getLanguageByCode(@PathVariable String code) {
        return languageRepository.findByCode(code)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    public ResponseEntity<Language> createLanguage(@RequestBody Language language) {
        Language saved = languageRepository.save(language);
        return ResponseEntity.ok(saved);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Language> updateLanguage(@PathVariable Long id, @RequestBody Language language) {
        return languageRepository.findById(id)
                .map(existing -> {
                    language.setId(id);
                    language.setCreatedAt(existing.getCreatedAt());
                    return ResponseEntity.ok(languageRepository.save(language));
                })
                .orElse(ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteLanguage(@PathVariable Long id) {
        if (languageRepository.existsById(id)) {
            languageRepository.deleteById(id);
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }
}

